import math
from ship_envs.core.entity import Entity
from ship_envs.core.rect_model import RectModel

import numpy as np


class Agent(Entity):
    def __init__(self):
        super(Agent, self).__init__()
        self.target = None
        self.model = RectModel()
        self.model.color = np.array([1, 0.75, 0.75])
        self.range = None
        self.cur_speed = 0.0
        self.dt = 1

    def step(self, action):
        # 暂时按离散动作进行测试
        # if action == 0: 动作0 不做改变
        if action == 1:
            self.model.rotate(5)
        elif action == 2:
            self.model.rotate(-5)
        self.model.move(self.cur_speed * self.dt)

    def set_range(self, range):
        self.range = range

    def arrive_target(self):
        return self.model.collision_with_other(self.target)
        # if not range:
        #     return range.out_range(self)
        # return False

    # 是否超出限制范围
    def out_range(self):
        if self.range:
            return self.range.out_range(self)
        return False

    # 获取当前的中心点
    def get_pos(self):
        return self.model.get_pos()

    def set_pos(self, pos):
        self.model.set_pos(pos)

    # 到target的向量
    def get_forward_target_vec(self):
        if self.target:
            return tuple(np.array(self.target.get_pos()) - np.array(self.model.get_pos()))
        return 0.0, 0.0
